/*
 * my_pwm.h
 *
 *  Created on: J 23, 2024
 *      Author: 86138
 */

#ifndef MY_PWM_H_
#define MY_PWM_H_
#include "zf_common_headfile.h"

#define CHANNEL_NUMBER          (6)

#define PWM_CH1                 (TIM4_PWM_MAP1_CH3_D14)//(TIM2_PWM_MAP0_CH2_A1)侧左
#define PWM_CH2                 (TIM2_PWM_MAP0_CH2_A1)//(TIM4_PWM_MAP1_CH2_D13)侧右
#define PWM_CH3                 (TIM4_PWM_MAP1_CH2_D13)//(TIM2_PWM_MAP0_CH3_A2)后左
#define PWM_CH4                 (TIM2_PWM_MAP0_CH3_A2)//(TIM4_PWM_MAP1_CH3_D14)后右
#define PWM_CH5                 (TIM3_PWM_MAP3_CH1_C6)
#define PWM_CH6                 (TIM1_PWM_MAP3_CH2_E11)//c6 c7 c8 c9
//encoder
#define PIT_CH                          (TIM5_PIT )                             // 使用的周期中断编号 如果修改 需要同步对应修改周期中断编号与 isr.c 中的调用
#define PIT_PRIORITY                    (TIM5_IRQn)                             // 对应周期中断的中断编号
#define ENCODER_QUADDEC                 (TIM10_ENCOEDER)                         // 正交编码器对应使用的编码器接口 这里使用QTIMER1的ENCOEDER1
#define ENCODER_QUADDEC_A               (TIM10_ENCOEDER_MAP3_CH1_D1)             // A 相对应的引脚
#define ENCODER_QUADDEC_B               (TIM10_ENCOEDER_MAP3_CH2_D3)            // B 相对应的引脚
//tuoluoyi
#define PIT                         (TIM6_PIT )
//电量
extern float energy;//实际电量
extern float energy_vef;//电量参考
//PWM
extern uint8 channel_index ;
extern pwm_channel_enum channel_list[CHANNEL_NUMBER];
extern int16 duty[CHANNEL_NUMBER];
extern int16 duty_v;
extern int16 toggle_flag[4];

//PID
 typedef struct PID_t {
    float prepreError;
    float preError;
    float error;
    float integral;
    float derivative;
    float output;
    float Kp;
    float Ki;
    float Kd;
    float target;
}PID_t;

extern PID_t PID_duoji;
extern PID_t PID_velocity;
extern PID_t PID_position;
extern PID_t PID_pos_v;
extern PID_t PID_pos_omega;
extern PID_t PID_pos_alpha;

//velocity
extern int16 v_ref;
extern int16 encoder_data_quaddec;
extern int16 v_real;
//omega
extern int omega_ref;
extern int omega_real;

void           Init                        (void);
void           control_velocity            (void);
void           control_position            (void);
void           control_position_omega      (void);
int            my_pwm                      (void);
void           my_pwm_set                  (void);
void           PID_Algorithm1              (struct PID_t* PID, float measuredVal);
void           PID_Algorithm2              (struct PID_t* PID, float measuredVal);

#endif /* MY_PWM_H_ */
